/*
 * Teknei 2014 - All rights reserved
 */
package com.teknei.acabus.core.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.apache.commons.collections.CollectionUtils;
import org.primefaces.model.DualListModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.teknei.acabus.FacadeException;
import com.teknei.acabus.core.common.MessageConstants;
import com.teknei.acabus.core.common.NavigationConstants;
import com.teknei.acabus.core.entities.NavigationParent;
import com.teknei.acabus.core.entities.Role;
import com.teknei.acabus.core.facade.NavigationFacade;

/**
 * Controller for NavigationParent and Role elements
 * @author Jorge Amaro
 * @version 1.0
 * @since 1.0
 *
 */
@ViewScoped
@ManagedBean(name="npr_controller")
public class NavigationParentRoleController extends GenericController implements Serializable{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@EJB
	private NavigationFacade facadeNavigation;
	private List<NavigationParent> itemsList;
	private List<Role> currentRolesParent;
	private NavigationParent currentItem;
	private static final Logger LOG = LoggerFactory.getLogger(NavigationParentRoleController.class);
	private String rolesAsigned;

	/**
	 * PostConstruct method. Initialize elements
	 */
	@PostConstruct
	private void postConstruct() {
		initItemList();
	}

	/**
	 * Initialize items lists
	 */
	private void initItemList(){
		try {
			itemsList = facadeNavigation.findNavigationParents();
		} catch (FacadeException e) {
			super.addErrorMessage(MessageConstants.ACTION_ERROR_INIT_LIST);
		}
	}
	
	/**
	 * Finds all the parent roles
	 */
	private void findParentRoles(){
		try {
			currentRolesParent = facadeNavigation.findRolesAllowed(currentItem);
			if(CollectionUtils.isEmpty(currentRolesParent)){
				rolesAsigned = "";
				return;
			}
			StringBuilder sb = new StringBuilder();
			for(Role role : currentRolesParent){
				sb.append(role.getRoleName());
				sb.append(", ");
			}
			int length = sb.length();
			sb.replace(length -2, length-1, " ");
			rolesAsigned = sb.toString();
		} catch (FacadeException e) {
			super.addErrorMessage(MessageConstants.ACTION_ERROR_INIT_LIST);
		}
	}
	
	/**
	 * Pre-loads the parent role items
	 */
	public void preViewItem(){
		findParentRoles();
	}
	
	
	/**
	 * Validates if there is an item to update. If so, redirects to the required page
	 * @return
	 */
	public String onEditRole(){
		if(currentItem == null){
			super.addInfoMessage(MessageConstants.WARNING_INPUT_SELECTION_REQUIRED);
			return NavigationConstants.NAVIGATION_SAME_PAGE;
		}
		super.getFacesContext().getExternalContext().getSessionMap().put(NavigationConstants.EDIT_NAVIGATION_PARENT_RULE, currentItem);
		return NavigationConstants.NAVIGATION_PARENT_ROLE_EDIT;
	}
	
	/**
	 * @return the facadeNavigation
	 */
	public NavigationFacade getFacadeNavigation() {
		return facadeNavigation;
	}

	/**
	 * @param facadeNavigation the facadeNavigation to set
	 */
	public void setFacadeNavigation(NavigationFacade facadeNavigation) {
		this.facadeNavigation = facadeNavigation;
	}

	/**
	 * @return the itemsList
	 */
	public List<NavigationParent> getItemsList() {
		initItemList();
		return itemsList;
	}

	/**
	 * @param itemsList the itemsList to set
	 */
	public void setItemsList(List<NavigationParent> itemsList) {
		this.itemsList = itemsList;
	}

	/**
	 * @return the currentRolesParent
	 */
	public List<Role> getCurrentRolesParent() {
		return currentRolesParent;
	}

	/**
	 * @param currentRolesParent the currentRolesParent to set
	 */
	public void setCurrentRolesParent(List<Role> currentRolesParent) {
		this.currentRolesParent = currentRolesParent;
	}

	/**
	 * @return the currentItem
	 */
	public NavigationParent getCurrentItem() {
		return currentItem;
	}

	/**
	 * @param currentItem the currentItem to set
	 */
	public void setCurrentItem(NavigationParent currentItem) {
		this.currentItem = currentItem;
	}


	/**
	 * @return the rolesAsigned
	 */
	public String getRolesAsigned() {
		return rolesAsigned;
	}

	/**
	 * @param rolesAsigned the rolesAsigned to set
	 */
	public void setRolesAsigned(String rolesAsigned) {
		this.rolesAsigned = rolesAsigned;
	}
}
